package cn.zzm.core.config;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.pool.PooledConnectionFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
import org.springframework.jms.connection.CachingConnectionFactory;
import org.springframework.jms.connection.SingleConnectionFactory;
import org.springframework.jms.core.JmsTemplate;

/**
 * TODO(带缓存的mq配置).
 *
 * <p>TODO(带缓存的mq配置).
 *
 * <p>版权所有:Copyright (c) 2019<br/>
 *
 * @author 周昭名
 * @version V1.0
 * @since 2019/7/1 19:01
 */
@Configuration
public class ActiveMQCloudConfig {
    @Value("${spring.activemq.cloud.broker-url}")
    private String brokerUrl;
    @Value("${spring.activemq.cloud.user}")
    private String user;
    @Value("${spring.activemq.cloud.password}")
    private String password;
    @Value("${spring.activemq.cloud.cacheConsumers}")
    private boolean cacheConsumers;
    @Value("${spring.activemq.cloud.cacheproducers}")
    private boolean cacheproducers;
    @Value("${spring.activemq.cloud.sessionCacheSize}")
    private int sessionCacheSize;

    /**
      *
      *TODO(activemq连接工厂).
      *
      *<p>TODO(activemq连接工厂).
      *
      *@return
      *@author 周昭名
      *@since 2019/7/26 16:43
    */
    @Bean(name="cloudActiveMQConnectionFactory")
    public ActiveMQConnectionFactory cloudActiveMQConnectionFactory(){
        ActiveMQConnectionFactory amf = new ActiveMQConnectionFactory(user,password,brokerUrl);
        return amf;
    }
    
    /**
      * 
      *TODO(带缓存的连接工厂).
      *
      *<p>TODO(带缓存的连接工厂).
      *
      *@return
      *@author 周昭名
      *@since 2019/7/26 16:44
    */
    @Bean(name="cloudCachConnectionFactory")
    public CachingConnectionFactory cloudCachingConnectionFactory(){
        CachingConnectionFactory cloudCachingConnectionFactory = new CachingConnectionFactory(cloudActiveMQConnectionFactory());
        cloudCachingConnectionFactory.setCacheConsumers(cacheConsumers);
        cloudCachingConnectionFactory.setCacheProducers(cacheproducers);
        cloudCachingConnectionFactory.setSessionCacheSize(sessionCacheSize);
        return cloudCachingConnectionFactory;
    }
    
    /**
      * 
      *TODO(单例的连接工厂 用于持久化订阅).
      *
      *<p>TODO(单例的连接工厂 用于持久化订阅).
      *
      *@return
      *@author 周昭名
      *@since 2019/7/26 16:44
    */
    @Bean(name="cloudSingleConnectionFactory")
    public SingleConnectionFactory cloudSingleConnectionFactory(){
        SingleConnectionFactory singleConnectionFactory = new SingleConnectionFactory(cloudActiveMQConnectionFactory());
        singleConnectionFactory.setTargetConnectionFactory(cloudActiveMQConnectionFactory());
        singleConnectionFactory.setClientId("CloudDurableTopic");
        return singleConnectionFactory;
    }

    /**
      *
      *TODO(jms queue listener工，用于新建@Listener时指定).
      *
      *<p>TODO(jms queue listener工，用于新建@Listener时指定).
      *
      *@return
      *@author 周昭名
      *@since 2019/7/26 16:45
    */
    @Bean(name="jmsCloudQueueListenerContainerFactory")
    public DefaultJmsListenerContainerFactory jmsCloudQueueListenerContainerFactory() {
        DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
        factory.setConnectionFactory(cloudCachingConnectionFactory());
        return factory;
    }

    /**
     *
     *TODO(jms topic listener工，用于新建@Listener时指定).
     *
     *<p>TODO(jms topic listener工，用于新建@Listener时指定).
     *
     *@return
     *@author 周昭名
     *@since 2019/7/26 16:45
     */
    @Bean(name="jmsCloudTopicListenerContainerFactory")
    public DefaultJmsListenerContainerFactory jmsCloudTopicListenerContainerFactory() {
        DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
        factory.setConnectionFactory(cloudCachingConnectionFactory());
        factory.setPubSubDomain(true);
        return factory;
    }
    /**
     *
     *TODO(jms 持久化listener工厂).
     *
     *<p>TODO(jms 持久化listener工厂).
     *
     *@return
     *@author 周昭名
     *@since 2019/7/26 16:45
     */
    @Bean(name="jmsCloudDurableTopicListenerContainerFactory")
    public DefaultJmsListenerContainerFactory jmsRemoteDurableTopicListenerContainerFactory() {
        DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
        factory.setConnectionFactory(cloudSingleConnectionFactory());
        factory.setPubSubDomain(true);
        factory.setSubscriptionDurable(true);
        factory.setSessionTransacted(true);
        return factory;
    }

    /**
     *
     *TODO(带缓存的jmsTemplate).
     *
     *<p>TODO(带缓存的jmsTemplate).
     *
     *@return
     *@author 周昭名
     *@since 2019/7/26 17:13
     */
    @Bean(name="jmsCloudTemplate")
    public JmsTemplate jmsCloudTemplate(){
        JmsTemplate jmsTemplate = new JmsTemplate();
        jmsTemplate.setConnectionFactory(cloudCachingConnectionFactory());
        return jmsTemplate;
    }
}
